//
//  TPWalletViewController.m
//  TradingPlatform
//
//  Created by 李坤 on 2019/5/15.
//  Copyright © 2019 程强. All rights reserved.
//

#import "TPWalletViewController.h"
#import "TPWalletManager.h"
#import "TPWalletAccountObj.h"
#import "UIImageView+WebCache.h"
#import "TPWalletInfoViewController.h"

@interface TPWalletViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (strong, nonatomic) TPWalletManager *manager;
@property (strong, nonatomic) TPWalletAccountObj *walletAccount;
@property (weak, nonatomic) IBOutlet UIView *unableWalletView;
@property (weak, nonatomic) IBOutlet UIView *ableWalletView;
@property (weak, nonatomic) IBOutlet UILabel *accountLbl;
@property (weak, nonatomic) IBOutlet UILabel *settlementCenterLbl;
@property (weak, nonatomic) IBOutlet UILabel *accountStatusLbl;
@property (weak, nonatomic) IBOutlet UILabel *timeLbl;
@property (weak, nonatomic) IBOutlet UILabel *accountNameLbl;
@property (weak, nonatomic) IBOutlet UIImageView *accountUserHeader;
@property (weak, nonatomic) IBOutlet UIButton *accountTypeBtn;
@property (weak, nonatomic) IBOutlet UIButton *activationBtn;
@end

@implementation TPWalletViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self fetchWalletAccountInfo];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的钱包";
    self.manager = [TPWalletManager new];
    [self setupUI];
}
- (void)setupUI
{
    self.scrollWidth.constant = ScreenWidth;
    self.scrollHeight.constant = ScreenHeight;
}
- (void)loadWalletInfo
{
    if (self.walletAccount.accountStatus == nil || [self.walletAccount.accountStatus isEqualToString:@""]){
        //未开户
        self.ableWalletView.hidden = YES;
        self.unableWalletView.hidden = NO;
        return;
    }
//    [TPGlobalClass shareIntance].accountStatus = self.walletAccount.accountStatus;
    self.ableWalletView.hidden = NO;
    if (![self.walletAccount.accountStatus isEqualToString:@"ENABLE"]) {
        //注册待激活
        self.accountStatusLbl.text = @"支付账号未激活";
        self.activationBtn.hidden = NO;
        self.timeLbl.hidden = YES;
    }else if ([self.walletAccount.accountStatus isEqualToString:@"ENABLE"]){
        //正常账号
        self.accountStatusLbl.text = @"支付账号已激活";
        self.activationBtn.hidden = YES;
        self.timeLbl.hidden = NO;
        self.timeLbl.text = [ToolKit timeStampTurnDate:self.walletAccount.time withFormat:@"yyyy-MM-dd"];
    }
    self.accountLbl.text = [NSString stringWithFormat:@"账号  %@",self.walletAccount.userAccount];
    self.settlementCenterLbl.text = [NSString stringWithFormat:@"结算中心：%@",self.walletAccount.Settlement];
    self.accountNameLbl.text = self.walletAccount.acctName;
    if ([self.walletAccount.acctType isEqualToString:@"PERSON"]) {
        [self.accountTypeBtn setTitle:@"个人" forState:UIControlStateNormal];
    }else{
        [self.accountTypeBtn setTitle:@"企业" forState:UIControlStateNormal];
    }
    [self.accountUserHeader sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:LoadImage(@"denglu")];
}
- (IBAction)openAccountClick:(id)sender
{
    weak_Self(self);
    [self accountOpeningSuccess:^(NSString *url) {
        TPWalletInfoViewController *vc = [[TPWalletInfoViewController alloc]init];
        vc.walletUrl = url;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}
- (IBAction)copyAccountClick:(id)sender
{
    NSString *account = self.walletAccount.userAccount;
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = account;
    [self showMessageHUD:@"已复制" delayHide:2.0];
}
- (IBAction)balanceClick:(id)sender
{
    weak_Self(self);
    [self showMessageHUD:@"正在跳转账户余额..."];
    [self getWalletBanlanceSuccess:^(NSString *wallet) {
        [weakSelf hideHUD];
        TPWalletInfoViewController *vc = [[TPWalletInfoViewController alloc]init];
        vc.walletUrl = wallet;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}
- (IBAction)activatyAccountClick:(id)sender
{
    weak_Self(self);
    [self getWalletBanlanceSuccess:^(NSString *wallet) {
        TPWalletInfoViewController *vc = [[TPWalletInfoViewController alloc]init];
        vc.walletUrl = wallet;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}
#pragma mark --- 获取开户信息
- (void)fetchWalletAccountInfo
{
    MyUser;
    weak_Self(self);
    NSString *type = [DECXGlobal shareInstance].loginType;
    if ([type isEqualToString:@"3"]) {
        type = @"1";
    }
    [[self.manager getMyWalletAccountInfo:user.userid withType:type]subscribeNext:^(id x) {
        weakSelf.walletAccount = x;
        if (x) {
            [weakSelf loadWalletInfo];
        }
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        [weakSelf showMessageHUD:msg delayHide:2.0];
    } completed:^{
        
    }];
}
#pragma mark --- 账号余额
- (void)getWalletBanlanceSuccess:(void(^)(NSString *wallet))block
{
    MyUser;
    weak_Self(self);
    NSString *type = [DECXGlobal shareInstance].loginType;
    if ([type isEqualToString:@"3"]) {
        type = @"1";
    }
    [[self.manager getMyWalletBalance:user.userid withType:type]subscribeNext:^(id x) {
        if (block) {
            block(x);
        }
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        [weakSelf showMessageHUD:msg delayHide:2.0];
    } completed:^{
        
    }];
}
#pragma mark --- 一键开户
- (void)accountOpeningSuccess:(void(^)(NSString *url))block
{
    MyUser;
    weak_Self(self);
    [self showHUD];
    NSString *type = [DECXGlobal shareInstance].loginType;
    if ([type isEqualToString:@"3"]) {
        type = @"1";
    }
    [[self.manager onekeyAccountOpening:user.userid withType:type]subscribeNext:^(id x) {
        if (block) {
            block(x);
        }
        [weakSelf hideHUD];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        [weakSelf showMessageHUD:msg delayHide:2.0];
    } completed:^{
        
    }];
}
@end
